home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Database Designers / Rational Rose 2000 / Rational Setup.EXE / common / lib / Win32 / Registry.pm < prev    next >
Text File  |  1998-11-15  |  11KB  |  431 lines

  1. package Win32::Registry;
  2. #######################################################################
  3. #Perl Module for Registry Extensions
  4. # This module creates an object oriented interface to the Win32
  5. # Registry.
  6. #
  7. # NOTE: This package exports the following "key" objects to
  8. # the main:: name space.
  9. #
  10. # $main::HKEY_CLASSES_ROOT
  11. # $main::HKEY_CURRENT_USER
  12. # $main::HKEY_LOCAL_MACHINE
  13. # $main::HKEY_USERS
  14. # $main::HKEY_PERFORMANCE_DATA
  15. # $main::HKEY_CURRENT_CONFIG
  16. # $main::HKEY_DYN_DATA
  17. #
  18. #######################################################################
  19.  
  20. require Exporter;
  21. require DynaLoader;
  22. use Win32::WinError;
  23.  
  24. $VERSION = '0.06';
  25.  
  26. @ISA= qw( Exporter DynaLoader );
  27. @EXPORT = qw(
  28.     HKEY_CLASSES_ROOT
  29.     HKEY_CURRENT_USER
  30.     HKEY_LOCAL_MACHINE
  31.     HKEY_PERFORMANCE_DATA
  32.     HKEY_CURRENT_CONFIG
  33.     HKEY_DYN_DATA
  34.     HKEY_USERS
  35.     KEY_ALL_ACCESS
  36.     KEY_CREATE_LINK
  37.     KEY_CREATE_SUB_KEY
  38.     KEY_ENUMERATE_SUB_KEYS
  39.     KEY_EXECUTE
  40.     KEY_NOTIFY
  41.     KEY_QUERY_VALUE
  42.     KEY_READ
  43.     KEY_SET_VALUE
  44.     KEY_WRITE
  45.     REG_BINARY
  46.     REG_CREATED_NEW_KEY
  47.     REG_DWORD
  48.     REG_DWORD_BIG_ENDIAN
  49.     REG_DWORD_LITTLE_ENDIAN
  50.     REG_EXPAND_SZ
  51.     REG_FULL_RESOURCE_DESCRIPTOR
  52.     REG_LEGAL_CHANGE_FILTER
  53.     REG_LEGAL_OPTION
  54.     REG_LINK
  55.     REG_MULTI_SZ
  56.     REG_NONE
  57.     REG_NOTIFY_CHANGE_ATTRIBUTES
  58.     REG_NOTIFY_CHANGE_LAST_SET
  59.     REG_NOTIFY_CHANGE_NAME
  60.     REG_NOTIFY_CHANGE_SECURITY
  61.     REG_OPENED_EXISTING_KEY
  62.     REG_OPTION_BACKUP_RESTORE
  63.     REG_OPTION_CREATE_LINK
  64.     REG_OPTION_NON_VOLATILE
  65.     REG_OPTION_RESERVED
  66.     REG_OPTION_VOLATILE
  67.     REG_REFRESH_HIVE
  68.     REG_RESOURCE_LIST
  69.     REG_RESOURCE_REQUIREMENTS_LIST
  70.     REG_SZ
  71.     REG_WHOLE_HIVE_VOLATILE
  72. );
  73.  
  74. @EXPORT_OK = qw(
  75.     RegCloseKey
  76.     RegConnectRegistry
  77.     RegCreateKey
  78.     RegCreateKeyEx
  79.     RegDeleteKey
  80.     RegDeleteValue
  81.     RegEnumKey
  82.     RegEnumValue
  83.     RegFlushKey
  84.     RegGetKeySecurity
  85.     RegLoadKey
  86.     RegNotifyChangeKeyValue
  87.     RegOpenKey
  88.     RegOpenKeyEx
  89.     RegQueryInfoKey
  90.     RegQueryValue
  91.     RegQueryValueEx
  92.     RegReplaceKey
  93.     RegRestoreKey
  94.     RegSaveKey
  95.     RegSetKeySecurity
  96.     RegSetValue
  97.     RegSetValueEx
  98.     RegUnLoadKey
  99. );
  100. $EXPORT_TAGS{ALL}= \@EXPORT_OK;
  101.  
  102. bootstrap Win32::Registry;
  103.  
  104. sub import
  105. {
  106.     my( $pkg )= shift;
  107.     if (  $_[0] && "Win32" eq $_[0]  ) {
  108.     Exporter::export( $pkg, "Win32", @EXPORT_OK );
  109.     shift;
  110.     }
  111.     Win32::Registry->export_to_level( 1+$Exporter::ExportLevel, $pkg, @_ );
  112. }
  113.  
  114. #######################################################################
  115. # This AUTOLOAD is used to 'autoload' constants from the constant()
  116. # XS function.  If a constant is not found then control is passed
  117. # to the AUTOLOAD in AutoLoader.
  118.  
  119. sub AUTOLOAD {
  120.     my($constname);
  121.     ($constname = $AUTOLOAD) =~ s/.*:://;
  122.     #reset $! to zero to reset any current errors.
  123.     $!=0;
  124.     my $val = constant($constname, @_ ? $_[0] : 0);
  125.     if ($! != 0) {
  126.     if ($! =~ /Invalid/) {
  127.         $AutoLoader::AUTOLOAD = $AUTOLOAD;
  128.         goto &AutoLoader::AUTOLOAD;
  129.     }
  130.     else {
  131.         ($pack,$file,$line) = caller;
  132.         die "Your vendor has not defined Win32::Registry macro $constname, used at $file line $line.";
  133.     }
  134.     }
  135.     eval "sub $AUTOLOAD { $val }";
  136.     goto &$AUTOLOAD;
  137. }
  138.  
  139. #######################################################################
  140. # _new is a private constructor, not intended for public use.
  141. #
  142.  
  143. sub _new
  144. {
  145.     my $self;
  146.     if ($_[0]) {
  147.     $self->{'handle'} = $_[0];
  148.     bless $self;
  149.     }
  150.     $self;
  151. }
  152.  
  153. #define the basic registry objects to be exported.
  154. #these had to be hardwired unfortunately.
  155. # XXX Yuck!
  156.  
  157. $main::HKEY_CLASSES_ROOT    = _new(&HKEY_CLASSES_ROOT);
  158. $main::HKEY_CURRENT_USER    = _new(&HKEY_CURRENT_USER);
  159. $main::HKEY_LOCAL_MACHINE    = _new(&HKEY_LOCAL_MACHINE);
  160. $main::HKEY_USERS        = _new(&HKEY_USERS);
  161. $main::HKEY_PERFORMANCE_DATA    = _new(&HKEY_PERFORMANCE_DATA);
  162. $main::HKEY_CURRENT_CONFIG    = _new(&HKEY_CURRENT_CONFIG);
  163. $main::HKEY_DYN_DATA        = _new(&HKEY_DYN_DATA);
  164.  
  165.  
  166. #######################################################################
  167. #Open
  168. # creates a new Registry object from an existing one.
  169. # usage: $RegObj->Open( "SubKey",$SubKeyObj );
  170. #               $SubKeyObj->Open( "SubberKey", *SubberKeyObj );
  171.  
  172. sub Open
  173. {
  174.     my $self = shift;
  175.     die 'usage: Open( $SubKey, $ObjRef )' if @_ != 2;
  176.     
  177.     my ($subkey) = @_;
  178.     my ($result,$subhandle);
  179.  
  180.     $result = RegOpenKey($self->{'handle'},$subkey,$subhandle);
  181.     $_[1] = _new( $subhandle );
  182.     
  183.     return 0 unless $_[1];
  184.     $! = Win32::GetLastError() unless $result;
  185.     return $result;
  186. }
  187.  
  188. #######################################################################
  189. #Close
  190. # close an open registry key.
  191. #
  192. sub Close
  193. {
  194.     my $self = shift;
  195.     die "usage: Close()" if @_ != 0;
  196.  
  197.     my $result = RegCloseKey($self->{'handle'});
  198.     $! = Win32::GetLastError() unless $result;
  199.     return $result;
  200. }
  201.  
  202. #######################################################################
  203. #Connect
  204. # connects to a remote Registry object, returning it in $ObjRef.
  205. # returns false if it fails.
  206. # usage: $RegObj->Connect( $NodeName, $ObjRef );
  207.  
  208. sub Connect
  209. {
  210.     my $self = shift;
  211.     die 'usage: Connect( $NodeName, $ObjRef )' if @_ != 2;
  212.      
  213.     my ($node) = @_;
  214.     my ($result,$subhandle);
  215.  
  216.     $result = RegConnectRegistry ($node, $self->{'handle'}, $subhandle);
  217.     $_[1] = _new( $subhandle );
  218.  
  219.     return 0 unless $_[1];
  220.     $! = Win32::GetLastError() unless $result;
  221.     return $result;
  222. }  
  223.  
  224. #######################################################################
  225. #Create
  226. # open a subkey.  If it doesn't exist, create it.
  227. #
  228.  
  229. sub Create
  230. {
  231.     my $self = shift;
  232.     die 'usage: Create( $SubKey,$ScalarRef )' if @_ != 2;
  233.  
  234.     my ($subkey) = @_;
  235.     my ($result,$subhandle);
  236.  
  237.     $result = RegCreateKey($self->{'handle'},$subkey,$subhandle);
  238.     $_[1] = _new ( $subhandle );
  239.  
  240.     return 0 unless $_[1];
  241.     $! = Win32::GetLastError() unless $result;
  242.     return $result;
  243. }
  244.  
  245. #######################################################################
  246. #SetValue
  247. # SetValue sets a value in the current key.
  248. #
  249.  
  250. sub SetValue
  251. {
  252.     my $self = shift;
  253.     die 'usage: SetValue($SubKey,$Type,$value )' if @_ != 3;
  254.     my $result = RegSetValue( $self->{'handle'}, @_);
  255.     $! = Win32::GetLastError() unless $result;
  256.     return $result;
  257. }
  258.  
  259. sub SetValueEx
  260. {
  261.     my $self = shift;
  262.     die 'usage: SetValueEx( $SubKey,$Reserved,$type,$value )' if @_ != 4;
  263.     my $result = RegSetValueEx( $self->{'handle'}, @_);
  264.     $! = Win32::GetLastError() unless $result;
  265.     return $result;
  266. }
  267.  
  268. #######################################################################
  269. #QueryValue  and QueryKey
  270. # QueryValue gets information on a value in the current key.
  271. # QueryKey "    "       "       "  key  "       "       "       
  272.  
  273. sub QueryValue
  274. {
  275.     my $self = shift;
  276.     die 'usage: QueryValue( $SubKey,$valueref )' if @_ != 2;
  277.     my $result = RegQueryValue( $self->{'handle'}, @_);
  278.     $! = Win32::GetLastError() unless $result;
  279.     return $result;
  280. }
  281.  
  282. sub QueryKey
  283. {
  284.     my $garbage;
  285.     my $self = shift;
  286.     die 'usage: QueryKey( $classref, $numberofSubkeys, $numberofVals )'
  287.         if @_ != 3;
  288.  
  289.     my $result = RegQueryInfoKey($self->{'handle'}, $_[0],
  290.                      $garbage, $garbage, $_[1],
  291.                      $garbage, $garbage, $_[2],
  292.                      $garbage, $garbage, $garbage, $garbage);
  293.  
  294.     $! = Win32::GetLastError() unless $result;
  295.     return $result;
  296. }
  297.  
  298. #######################################################################
  299. #QueryValueEx
  300. # QueryValueEx gets information on a value in the current key.
  301.  
  302. sub QueryValueEx
  303. {
  304.     my $self = shift;
  305.     die 'usage: QueryValueEx( $SubKey,$type,$valueref )' if @_ != 3;
  306.     my $result = RegQueryValueEx( $self->{'handle'}, $_[0], NULL, $_[1], $_[2] );
  307.     $! = Win32::GetLastError() unless $result;
  308.     return $result;
  309. }
  310.  
  311. #######################################################################
  312. #GetKeys
  313. #Note: the list object must be passed by reference: 
  314. #       $myobj->GetKeys( \@mylist )
  315. sub GetKeys
  316. {
  317.     my $self = shift;
  318.     die 'usage: GetKeys( $arrayref )' if @_ != 1 or ref($_[0]) ne 'ARRAY';
  319.  
  320.     my ($result, $i, $keyname);
  321.     $keyname = "DummyVal";
  322.     $i = 0;
  323.     $result = 1;
  324.     
  325.     while ( $result ) {
  326.     $result = RegEnumKey( $self->{'handle'},$i++, $keyname );
  327.     if ($result) {
  328.         push( @{$_[0]}, $keyname );
  329.     }
  330.     }
  331.     return(1);
  332. }
  333.  
  334. #######################################################################
  335. #GetValues
  336. # GetValues creates a hash containing 'name'=> ( name,type,data )
  337. # for each value in the current key.
  338.  
  339. sub GetValues
  340. {
  341.     my $self = shift;
  342.     die 'usage: GetValues( $hashref )' if @_ != 1;
  343.  
  344.     my ($result,$name,$type,$data,$i);
  345.     $name = "DummyVal";
  346.     $i = 0;
  347.     while ( $result=RegEnumValue( $self->{'handle'},
  348.                   $i++,
  349.                   $name,
  350.                   NULL,
  351.                   $type,
  352.                   $data ))
  353.     {
  354.     $_[0]->{$name} = [ $name, $type, $data ];
  355.     }
  356.     return(1);
  357. }
  358.  
  359. #######################################################################
  360. #DeleteKey
  361. # delete a key from the registry.
  362. #  eg: $CLASSES_ROOT->DeleteKey( "KeyNameToDelete");
  363. #
  364.  
  365. sub DeleteKey
  366. {
  367.     my $self = shift;
  368.     die 'usage: DeleteKey( $SubKey )' if @_ != 1;
  369.     my $result = RegDeleteKey($self->{'handle'}, @_);
  370.     $! = Win32::GetLastError() unless $result;
  371.     return $result;
  372. }
  373.  
  374. #######################################################################
  375. #DeleteValue
  376. # delete a value from the current key in the registry
  377. #  $CLASSES_ROOT->DeleteValue( "\000" );
  378.  
  379. sub DeleteValue
  380. {
  381.     my $self = shift;
  382.     die 'usage: DeleteValue( $SubKey )' if @_ != 1;
  383.     my $result = RegDeleteValue($self->{'handle'}, @_);
  384.     $! = Win32::GetLastError() unless $result;
  385.     return $result;
  386. }
  387.  
  388. #######################################################################
  389. #save
  390. #saves the current hive to a file.
  391. #
  392.  
  393. sub Save
  394. {
  395.     my $self = shift;
  396.     die 'usage: Save( $FileName )' if @_ != 1;
  397.     my $result = RegSaveKey($self->{'handle'}, @_);
  398.     $! = Win32::GetLastError() unless $result;
  399.     return $result;
  400. }
  401.  
  402. #######################################################################
  403. #Load
  404. #loads a saved key from a file.
  405.  
  406. sub Load
  407. {
  408.     my $self = shift;
  409.     die 'usage: Load( $SubKey,$FileName )' if @_ != 2;
  410.     my $result = RegLoadKey($self->{'handle'}, @_);
  411.     $! = Win32::GetLastError() unless $result;
  412.     return $result;
  413. }
  414.  
  415. #######################################################################
  416. #UnLoad
  417. #unloads a registry hive
  418.  
  419. sub UnLoad
  420. {
  421.     my $self = shift;
  422.     die 'usage: UnLoad( $SubKey )' if @_ != 1;
  423.     my $result = RegUnLoadKey($self->{'handle'}, @_);
  424.     $! = Win32::GetLastError() unless $result;
  425.     return $result;
  426. }
  427. #######################################################################
  428.  
  429. 1;
  430. __END__
  431.